package com.herui.sdyu_lib.blu;

import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;

import com.herui.sdyu_lib.utils.LogUtils;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

/**
 * Created by amanda.shan on 2017/8/25.
 */

public class DemoBluService extends BaseBluService {

    private LocalBroadcastManager localBroadcastManager;

    private Context context;

    public DemoBluService(Context context) {
        super(context);
        this.context = context;

        localBroadcastManager = LocalBroadcastManager.getInstance(context); // 获取实例\

        setListener(new MsgListener() {
            @Override
            public void onDeviceState(BlueDevice blueDevice, int connectState) {
                LogUtils.i(String.format("设备:%s 连接状态改变:%d", blueDevice.getBlueName(),
                        connectState));
                sendMsg();
            }
        });
    }

    @Override
    public void reciveBlu(byte[] value, int len) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < len; ++i) {
            builder.append(String.format("%x ", value[i]));
        }
        LogUtils.d("收到数据: " + builder.toString());

    }


    private void sendMsg(){

        // 發送廣播通知
        Intent intent = new Intent("org.shan.smartwatch.service.GunBluService" +
                ".LOCAL_BROADCAST");
        intent.putExtra("connectState", connectState);
        localBroadcastManager.sendBroadcast(intent); // 发送本地广播
    }

}
